@tool
class_name AdditionalEffect_Timer
extends AdditionalEffect

## 设置Timer

enum AdditionalEffectTimerSignal
{
	Start,
	Tick,
	Done,
	Cancel,
	Completecd
}
#region 属性
@export var duration_range:Vector2
@export var repeat_count:int = 1
@export var connect_flag:ConnectFlags = 0
@export var additional_effect_dict:Dictionary[AdditionalEffectTimerSignal,AdditionalEffectInfo]
var timer:CustomTimer
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(args:Dictionary = {}):
	if super(args):
		for additional_effect_info:AdditionalEffectInfo in additional_effect_dict.values():
			for additional_effect:AdditionalEffect in additional_effect_info.addition_effects:
				additional_effect.init(args)
		return true
	return false
## 检查能否执行添加效果，默认为true
func can_excute() -> bool:
	return super()
## 执行添加效果
func excute() -> bool:
	if super():
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		timer = GameCustomTimerSystem.create_timer(GameManager.get_grng().grng_randf_range(duration_range.x,duration_range.y),repeat_count)
		timer.register_listener(timer.timer_start,func():
			var additional_effect_info:AdditionalEffectInfo = additional_effect_dict.get(AdditionalEffectTimerSignal.Start,null)
			if additional_effect_info:
				for additional_effect:AdditionalEffect in additional_effect_info.addition_effects:
					additional_effect.excute()
			,connect_flag)
		timer.register_listener(timer.timer_tick,func():
			var additional_effect_info:AdditionalEffectInfo = additional_effect_dict.get(AdditionalEffectTimerSignal.Tick,null)
			if additional_effect_info:
				for additional_effect:AdditionalEffect in additional_effect_info.addition_effects:
					additional_effect.excute()
			,connect_flag)
		timer.register_listener(timer.timer_done,func(idx:int):
			var additional_effect_info:AdditionalEffectInfo = additional_effect_dict.get(AdditionalEffectTimerSignal.Done,null)
			if additional_effect_info:
				for additional_effect:AdditionalEffect in additional_effect_info.addition_effects:
					additional_effect.excute()
			,connect_flag)
		timer.register_listener(timer.timer_finished,func(timer_result:CustomTimer.TimerResult):
			if timer_result == CustomTimer.TimerResult.Cancel:
				var additional_effect_info:AdditionalEffectInfo = additional_effect_dict.get(AdditionalEffectTimerSignal.Cancel,null)
				if additional_effect_info:
					for additional_effect:AdditionalEffect in additional_effect_info.addition_effects:
						additional_effect.excute()
			elif timer_result == CustomTimer.TimerResult.Completed:
				var additional_effect_info:AdditionalEffectInfo = additional_effect_dict.get(AdditionalEffectTimerSignal.Completecd,null)
				if additional_effect_info:
					for additional_effect:AdditionalEffect in additional_effect_info.addition_effects:
						additional_effect.excute()
			,connect_flag)
		timer.start_timer()
		return true
	else:
		return false
## 取消执行的效果
func cancel_excute() -> bool:
	if super():
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		return true
	else:
		return false
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
#endregion
